<?php
// /公共控制器
class CommonController extends Controller{
     
          
     public function __init(){
    // 为了不覆盖父类contorller的__init()方法
      parent::__init();
      $this->cate();
      $this->menu();

   }
   

   // 二级菜单
    public function cate(){
       
    	$model=K('Category');
        $data=Data::channelLevel($model->all(), $pid = 0, $html = "&nbsp;", 'cid', 'pid', $level = 1);
          // p($data);die;
        $this->assign('data',$data);

    }


 // 横向顶级菜单
 public function menu(){
    $menu=K('Category')->field('cid,ctitle,csort')->where('pid=0')->order('csort asc')->limit('0,6')->select();
     $this->assign('menu',$menu);
      // p($menu);die;
 }   

// 商品搜索
 // public function search(){
 //   $search=K('Goods')->


 // }



//递归读取子栏目分类
    public function getCates($pid){
    	$cate = K('Category');
    	$cates = $cate->where("pid={$pid}")->select();
    	if(empty($cates)) return ;
    	$sub = array();
    	foreach ($cates as $k => $v) {
    		$v['son'] = $this->getCates($v['cid']);
    		$sub[] = $v;
    	}
    
    	return $sub;
    }

  



// 取出对应栏目的子集（包括所有子集分类）
public function getCate($cid){
 $category=K('Category');

    $cids=$category->getSon($category->all(),$cid);
    // 把自己也压入
    $cids[]=$cid;
   $cids=implode(',',$cids);
  return  $cids;

}



// 处理面包屑导航
public function crumb($cid){
     // $cid=Q('get.cid',0,'intval');
         $data=K('Category')->all();
         // 获得全部父级递归处理
         $crumb=$this->getFather($data,$cid);
         // 数组反转,获得自己分类
         $crumb=array_reverse($crumb);
         $this->assign('crumb',$crumb);

}


// 获得父级分类
  public function getFather($data,$cid){
     $temp=array();
     foreach($data as $v){
      if($v['cid']==$cid){
        $temp[]=$v;
        $temp=array_merge($temp,$this->getFather($data,$v['pid']));
      }
     }
     

     return $temp;
   
  }


// hover购物车

  public function hoverCart(){
    $data=$_SESSION['cart'];
    foreach ($data['goods'] as $k => $v) {
      $gid=$v['id'];
      $data['goods'][$k]['good']=K("Goods")->where("goods_id={$gid}")->find();
     
    }
    $this->ajax($data);

  }

// 异步删除
// public function delcart(){
//     $sid=Q("post.sid");
//     $data=array(
//       'sid'=>$sid,
//       'num'=>0
//     );
//     Cart::update($data);
//   }


}


 